#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include <stdlib.h>
#define max 100

// Khai bao
typedef struct sinhvien
{
   char hoten[35],ms[5], dtoan[3],dly[3], dhoa[3];
   float dtb;

}sv;
sv ds[max],b[max];;
char hoten[35],ms[5],tl,mssv[5];
int n=0;
// Kiem tra diem so nhap vao
int checkDouble(char var[3])
{
   int isdouble=0;
   for(int i=0,len=strlen(var);i<len;i++)
   {
      if('0'<=var[i] && var[i]<='9'||var[i]=='.');
      else
	 isdouble++;
   }
   if(isdouble==0)
   {
      if((float)strtod(var,NULL)<0 || (float)strtod(var,NULL)>10)
	 isdouble=1;
   }
   return isdouble;
}
// Kiem tra ma so, tra ve vi tri
int search(char mssv[5])
{
   int index=-1;
   for(int i=0;i<n;i++)
   {
      if(strcmp(mssv,ds[i].ms)==0)
      {
	 index=i;
	 break;
      }
   }
   return index;
}
// Nhap thong tin sinh vien
void nhapthongtinsv(int index)
{
   cout<<"\nNhap ho va ten sinh vien: ";
   gets(hoten);
   strcpy(ds[index].hoten, hoten);
   cout<<"\nNhap diem: ";
   cout<<"\n\tNhap diem Toan : ";
   cin>>ds[index].dtoan;
   while(checkDouble(ds[index].dtoan))
   {
      cout<<"\n\tThong bao: Nhap lai diem Toan : ";
      cin>>ds[index].dtoan;
   }
   cout<<"\n\tNhap diem Ly : ";
   cin>>ds[index].dly;
   while(checkDouble(ds[index].dly))
   {
      cout<<"\n\tThong bao: Nhap lai diem Ly : ";
      cin>>ds[index].dly;
   }
   cout<<"\n\tNhap diem Hoa : ";
   cin>>ds[index].dhoa;
   while(checkDouble(ds[index].dhoa))
   {
      cout<<"\n\tThong bao: Nhap lai diem Hoa : ";
      cin>>ds[index].dhoa;
   }
   ds[index].dtb=((float)strtod(ds[index].dtoan,NULL)+(float)strtod(ds[index].dly,NULL)+(float)strtod(ds[index].dhoa,NULL))/3;
   cout<<"\n";
   cout<<"--------------------------------------------";
}
// Nhap moi danh sach sinh vien
void nhapmoi()
{
   int index=-1,dodai;
   do
   {
      cout<<"\nNhap ma so sinh vien (trong de thoat): ";
      gets(ds[n].ms);
      index=search(ds[n].ms);
      while(index!=-1)
      {
	 cout<<"\n\tThong bao:Ma so sinh vien nay da co trong danh sach!";
	 cout<<"\nHay nhap lai ma so sinh vien: ";
	 gets(ds[n].ms);
	 index=search(ds[n].ms);
      }
      dodai=strlen(ds[n].ms);
      while(dodai>5)
      {
	 cout<<"\n\tThong bao:Ma so sinh vien nhap vao khong phu hop!";
	 cout<<"\nNhap lai ma so sinh vien (<=5 ky tu) : ";
	 gets(ds[n].ms);
	 dodai=strlen(ds[n].ms);
      }
      if(dodai>0&&dodai<=5)
      {
	 nhapthongtinsv(n);
	 n++;
      }
   }while(dodai>0&&dodai<6);
}
// Thong tin sinh vien
void inthongtinsv(int index)
{
   cout<<"\n\n";
   cout<<"\n\tHo & ten sinh vien: "<<ds[index].hoten;
   cout<<"\n\tMa so sinh vien: "<<ds[index].ms;
   cout<<"\n\tDiem mon Toan  : "<<ds[index].dtoan;
   cout<<"\n\tDiem mon Ly    : "<<ds[index].dly;
   cout<<"\n\tDiem mon Hoa   : "<<ds[index].dhoa;
   cout.precision(2);cout<<"\n\tDiem trung binh: "<<ds[index].dtb;
   cout<<"\n";
   cout<<"-----------------------------------------------------";
}
// Liet ke danh sach cac sinh vien
void inds()
{
   if (n!=0)
   {
      for (int index=0; index<n; index++)
      {
	 cout<<"\nSinh vien thu "<< (index+1)<<": ";
	 inthongtinsv(index);
      }
   }
   else cout<<"\n Danh sach hien chua co sinh vien nao!";
   getch();
}
// Them sinh vien
void them()
{
   cout<<"\nThong bao: Ban co muon them vao sinh vien moi (C/K): ";
   cin>>tl;
   if (tl=='c'||tl=='C')
      nhapmoi();
   else
   cout<<"\nThong bao: Viec them moi bi huy bo!";
   getch();
}
// Sua thong tin sinh vien
void sua(int index)
{
   cout<<"\nThong bao: Ban co chac muon sua sinh vien nay (C/K): ";
   cin>>tl;
   if (tl=='c'||tl=='C')
   {
      nhapthongtinsv(index);
      cout<<"\nThong bao: Da thay doi thanh cong!";
   }
   else
   cout<<"\nThong bao: Viec sua bi huy bo!";
   getch();
}
// Xoa sinh vien
void xoa(int index)
{
   cout<<"\nThong bao: Ban co chac muon xoa sinh vien nay (C/K): ";
   cin>>tl;
   if (tl=='c'||tl=='C')
   {
      for (int k=index; k<n-1; k++)
	 ds[k] = ds[k+1];
      n--;
      cout<<"\nThong bao: Da xoa thanh cong! ";
   }
   else
      cout<<"\nThong bao: Viec xoa bi huy bo!";
   getch();
}
// Tim kiem theo ma so sinh vien
int timkiem(char ms[5])
{
   int done=0,i=-1;
   while(!done)
   {
      cout<<"\nNhap ma so sinh vien: ";
      gets(ms);
      if(strlen(ms)==0 || strlen(ms)>5)
	 done=0;
      else
	 done=1;
   }
   if(done)
   {
      i=search(ms);
      if(i==-1)
	 cout<<"\nThong bao: Khong tim thay sinh vien nao co ma so "<<ms ;
      else
      {
	 inthongtinsv(i);
      }
   }
   return i;
}
// Tim kiem theo ten sinh vien
void timkiemten()
{
   int i, k, tim[max];
   char  timhoten[35];
   cout<<"\nNhap ho va ten sinh vien can tim (bo trong de thoat) : ";
   gets(timhoten);
   if(strlen(timhoten)!=0)
   {
      if (n!= 0)
      {
	 i=k=0;
	 do
	 {
	    if(strcmp(ds[i].hoten,timhoten) == 0)
	       tim[k++] = i;
	    i++;
	 }while (i<n);
	 if (!k)
	    cout<<"\nKhong tim thay hoc sinh co ten "<<timhoten;
	 else
	 {
	    cout<<"\nTim thay "<<k<<" sinh vien co ten "<<timhoten;
	    for (i=0; i<k; i++)
	    {
	       cout<<"\n\tHo & Ten       : "<<ds[tim[i]].hoten;
	       cout<<"\n\tMa So          : "<<ds[tim[i]].ms;
	       cout<<"\n\tDiem toan      : "<<ds[tim[i]].dtoan;
	       cout<<"\n\tDiem ly        : "<<ds[tim[i]].dly;
	       cout<<"\n\tDiem hoa       : "<<ds[tim[i]].dhoa;
	       cout<<"\n\tDiem trung binh: "<<ds[tim[i]].dtb;
	       cout<<"\n";
	       cout<<"----------------------------------------------------";
	    }
	 }
      }
      else
	 cout<<"\nHien tai chua co sinh vien nao trong danh sach!";
   }getch();
}
// Ghi file
void ghidulieu()
{
  FILE *f;
  f = fopen("qlsv.bdf", "wb");
  fwrite(ds, sizeof(ds),1,f);
  fclose(f);
  cout<<"\nGhi duoc "<<n<<" du lieu";
  getch();
}
// Doc file - xuat mang
void docdulieu()
{
  FILE *f;
  long  vt;
  f=fopen("qlsv.bdf", "rb");
  while(!feof(f))
  {
     if(fread(ds, sizeof(ds),1,f)!=0)
	inds();
  }
  fclose(f);
}
//Chuong trinh chinh
void main()
{
   clrscr();
   int i,cv;
   char tl;
   do
   {
      clrscr();
      int index=-1;
      cout<<"\n";
      cout<<"\n---------DO AN CO SO------------\n";
      cout<<"\n-------------MENU---------------";
      cout<<"\n";
      cout<<"\n QUAN LY SINH VIEN\n ";
      cout<<"\n   *1.Nhap moi danh sach ";
      cout<<"\n   *2.Liet ke danh sach ";
      cout<<"\n---------------------------------";
      cout<<"\n";
      cout<<"\n CAP NHAT THONG TIN SINH VIEN \n";
      cout<<"\n   *3.Them sinh vien";
      cout<<"\n   *4.Sua thong tin sinh vien";
      cout<<"\n   *5.Xoa 1 sinh vien";
      cout<<"\n---------------------------------";
      cout<<"\n";
      cout<<"\n TIM KIEM\n";
      cout<<"\n   *6.Tim kiem sinh vien ";
      cout<<"\n---------------------------------";
      cout<<"\n";
      cout<<"\n LUU TRU DU LIEU\n";
      cout<<"\n   *7.Ghi du lieu";
      cout<<"\n   *8.Doc du lieu";
      cout<<"\n---------------------------------";
      cout<<"\n";
      cout<<"\n   *9.Sinh vien thuc hien";
      cout<<"\n---------------------------------";
      cout<<"\n";
      cout<<"\n   *0.Thoat";
      cout<<"\n---------------------------------" ;
      cout<<"\n *=> Chon cong viec ban can lam : ";
      cin>>cv;
      switch(cv)
      {
	 case 1: nhapmoi();break;
	 case 2: inds();break;
	 case 3: them();break;
	 case 4:
		 index= timkiem(" can sua: ");
		 if(index > -1) sua(index);
		 break;
	 case 5:
		 index= timkiem(" can xoa: ");
		 if(index > -1) xoa(index);
		 break;
	 case 6:
		 cout<<"\n 1.Tim kiem sinh vien theo ma so ";
		 cout<<"\n 2.Tim kiem sinh vien theo ho va ten ";
		 cout<<"\n 3.Thoat ";
		 cout<<"\n";
		 cout<<"\n Ban hay chon cong viec: ";
		 cin>>cv;
		 switch(cv)
		 {
		    case 1: index=timkiem(" can tim kiem: ");
			    if(index>0)
			    {
			       cout<<"\n ------------------------------";
			       cout<<"\n Hay chon cong viec tiep theo: ";
			       cout<<"\n   1.Sua  	       ";
			       cout<<"\n   2.Xoa  	       ";
			       cout<<"\n   3.Thoat	       ";
			       cout<<"\n ------------------------------";
			       cout<<"\n Cong viec: ";
			       cin>>cv;
			       switch(cv)
			       {
				  case 1:
					 if(index > -1) sua(index);
					 break;
				  case 2:
					 if(index>-1) xoa(index);
					 break;
				  case 3: break;
			       }getch();
			    }break;
		    case 2:
			    timkiemten();break;
		    case 3: break;
		 }break;
	 case 7:
		 ghidulieu();break;
	 case 8:
		 docdulieu();break;
	 case 9:
		 cout<<"\n ********************************************";
		 cout<<"\n #                DO AN CO SO               #";
		 cout<<"\n #             ------*****------            #";
		 cout<<"\n #                CHUONG TRINH              #";
		 cout<<"\n #             QUAN LY SINH VIEN            #";
		 cout<<"\n #   De tai: Quan Ly Sinh Vien Bang Mang    #";
		 cout<<"\n #   SVTH  : Luu Duc Thien - Lop K15TPM1    #";
		 cout<<"\n #   GVHD  : Th.S Vo Van Luong              #";
		 cout<<"\n #     ==> An Enter de ve Menu chinh <==    #";
		 cout<<"\n ********************************************";
		 getch();
      }
   }while(cv>0&&cv<10);
}